package com.zhangmeng.tools.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.zhangmeng.tools.utils.AlertUtils;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

@Slf4j
public class PasswordController {

    @FXML
    public Button save;


    @FXML
    public Button search;

    private JdbcTemplate jdbcTemplate;

    @FXML
    public TextField url;

    @FXML
    public TextField username;

    @FXML
    public TextField password;

    @FXML
    public TextField remark;

    @FXML
    public void initialize() {

        search.setOnAction(event -> {

            if (url.getText().length() == 0 ){
                AlertUtils.alert_warning("网址不能为空!");
                return;
            }

            getJdbcTemplate();

            queryDatabase(url.getText());

        });

        save.setOnAction(event -> {

            if (username.getText().length() == 0 ){
                AlertUtils.alert_warning("用户名不能为空!");
                return;
            }
            if (password.getText().length() == 0 ){
                AlertUtils.alert_warning("密码不能为空!");
                return;
            }

            if (url.getText().length() == 0 ){
                AlertUtils.alert_warning("网址不能为空!");
                return;
            }

            if (remark.getText().length() == 0 ){
                AlertUtils.alert_warning("备注不能为空!");
                return;
            }
            getJdbcTemplate();
            List<UserInfo> userInfos = queryDatabase(url.getText(), false);
            boolean flag= false;
            if (userInfos != null && userInfos.size() > 0 ){
                flag = true;
            }
            if (flag){
                updateUserInfo(url.getText(),username.getText(),password.getText(),remark.getText());
            }else {
                saveUserInfo(username.getText(),password.getText(),url.getText(),remark.getText());
            }

        });
    }

    private String ip_address = "123.57.75.116";
    private int port = 13306;
    private String data_base = "user_password";
    private String data_base_username = "root";
    private String data_base_password = "root";

    private JdbcTemplate getJdbcTemplate() {
        if (jdbcTemplate == null) {
            jdbcTemplate = new JdbcTemplate();
            DruidDataSource datasource = new DruidDataSource();
            String url = "jdbc:mysql://" + ip_address + ":" + port + "/" + data_base + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true";
            String driverClassName = "com.mysql.cj.jdbc.Driver";
            datasource.setUrl(url);
            datasource.setUsername(data_base_username);
            datasource.setPassword(data_base_password);
            datasource.setDriverClassName(driverClassName);
            jdbcTemplate.setDataSource(datasource);
        }
        return jdbcTemplate;
    }

    public void queryDatabase(String url) {
        String sql = "SELECT * FROM user_info WHERE url = ?";
        String urlToFind = "www.baidu.com"; // 你想查询的 URL

        if (url != null ){
            urlToFind = url;
        }

        List<UserInfo> userInfoList = jdbcTemplate.query(sql, new Object[]{urlToFind}, new BeanPropertyRowMapper<>(UserInfo.class));

        // 处理查询结果
//        for (UserInfo userInfo : userInfoList) {
//            System.out.println(userInfo);
//        }

        if (userInfoList.size() > 0 ){
            UserInfo userInfo = userInfoList.get(0);
            password.setText(userInfo.getPassword());
            username.setText(userInfo.getUsername());
            this.url.setText(userInfo.getUrl());
            remark.setText(userInfo.getRemark());
        }

        log.info("userInfoList:" + userInfoList.size());
    }

    public List<UserInfo> queryDatabase(String url,boolean flag) {
        String sql = "SELECT * FROM user_info WHERE url = ?";
        String urlToFind = "www.baidu.com"; // 你想查询的 URL

        if (url != null ){
            urlToFind = url;
        }

        List<UserInfo> userInfoList = jdbcTemplate.query(sql, new Object[]{urlToFind}, new BeanPropertyRowMapper<>(UserInfo.class));

        return userInfoList;
    }

    public void saveUserInfo(String username,String password,String url, String remark) {
        String sql = "INSERT INTO user_info (username,password,url, remark) VALUES (?, ? , ? , ? )";
        // 使用 jdbcTemplate.update 方法来执行插入操作
        int result = jdbcTemplate.update(sql, username, password,url,remark);
        if (result > 0) {
           log.info("数据插入成功!");
           AlertUtils.alert_msg("数据插入成功!");
        } else {
            log.info("数据插入失败!");
            AlertUtils.alert_msg("数据插入失败!");
        }
    }

    public void updateUserInfo(String url, String username, String password, String remark) {

        String sql = "UPDATE user_info SET username = ?, password = ?, remark = ? WHERE url = ?";

        // 使用 jdbcTemplate.update 方法来执行更新操作
        int result = jdbcTemplate.update(sql, username, password, remark, url);

        if (result > 0) {
            log.info("数据更新成功!");
            AlertUtils.alert_msg("数据更新成功!");
        } else {
            log.info("数据更新失败!");
            AlertUtils.alert_msg("数据更新失败!");
        }
    }

    public static class UserInfo {

        private String url;
        private String remark;

        private String username;

        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }
    }
}
